// pages/addinfor/addinfor.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img:"../../../img/tupian.png",
    pic:'',
    info:{},
    cate:{},
    index:0,
    place:{},
    index1:0,
    stime:'2020-01-01',
    array: ['使用中', '正常', '破损','报废', '待维修','借出'],
    kkk:0,
    show:1,
    btime:'2020-01-01',
    arr:['name','num','owner','brand','place','snum','stime','sale','type','price']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options){
    if(!wx.getStorageSync('uid')){
      wx.redirectTo({
        url: '/pages/login/login',
      })
    }
    this.getCate();
    this.getPlace();
  },
  getCate:function(){
    var that=this;
    wx.request({
      url: app.globalData.url+'api/cate/list',
      method: 'get', 
      success:function(res){
        that.setData({
          cate:res.data.data
        })
      }
    })
  },
  getPlace:function(){
    var that=this;
    wx.request({
      url: app.globalData.url+'api/info/place',
      method: 'get', 
      success:function(res){
        that.setData({
          place:res.data.data
        })
      }
    })
  },
  inputedit:function(e){
    let dataset = e.currentTarget.dataset;
    //data-开头的是自定义属性，可以通过dataset获取到，dataset是一个json对象，有obj和item属性，可以通过这两个实现双向数据绑定，通过更改这两个值，对不同name的变量赋值
    let value = e.detail.value;
    this.data[dataset.obj][dataset.item] = value;
    this.setData({
      info: this.data[dataset.obj]
    });
  },
  bindPickerChange: function(e){
    this.setData({
      index: e.detail.value
    })
    let info = this.data.info;
    info.cid = this.data.cate[e.detail.value]['id'];
    this.setData({
      info: info
    })
  },
  bindPickerChange1:function(e){
    this.setData({
      index1: e.detail.value
    })
    let info = this.data.info;
    info.place = this.data.place[e.detail.value]['id'];
    this.setData({
      info: info
    })
  },
  bindDateChange:function(e){
    let info = this.data.info;
    info.stime  = e.detail.value;
    this.setData({
      info: info,
      stime:e.detail.value
    })
  },
  bindDateChange1:function(e){
    let info = this.data.info;
    info.btime  = e.detail.value;
    this.setData({
      info: info,
      btime:e.detail.value
    })
  },
  bindPickerChange2:function(e){
    let info = this.data.info;
    info.type  = this.data.array[e.detail.value];
    if(info.type == '借出'){
      this.setData({
        show:2
      })
    }else{
      this.setData({
        show:1
      })
    }
    this.setData({
      info: info,
      kkk:e.detail.value
    })
  },
  submit:function(){
    var info = this.data.info;
    info.cid = this.data.cate[this.data.index]['id']
    info.place = this.data.place[this.data.index1]['id']
    info.stime = this.data.stime
    info.type = this.data.array[this.data.kkk]
    if(info.type == '借出'){
      info.btime = this.data.stime
      if(!info.borrow){
        wx.showToast({
          title: '请输入合法参数',
          icon:'none'
        })
        return false;
      }
    }
    for(var index in this.data.arr){
      if(!info[this.data.arr[index]] && this.data.arr[index] !== 'brand'){
        wx.showToast({
          title: '请输入合法参数',
          icon:'none'
        })
        return false;
      }
    }
    if(this.data.pic){
      info.img = this.data.pic;
    }
    wx.request({
      url: app.globalData.url+'api/zc/add',
      method: 'post',
      data:info,
      success:function(res){
        wx.showToast({
          title: '添加成功',
          duration:1000,
          success:function(){
            setTimeout(function () {
              //要延时执行的代码
              wx.navigateBack({//返回
                delta: 1
              })
            }, 1000) //延迟时间
          }
        })
      }
    })
  },
  upload:function(){
    var that =this;
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: app.globalData.url+'api/zc/upload', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'img',
          formData:{},
          success: function(res){
            wx.showToast({
              title: '上传成功',
            })
            that.setData({
              img:tempFilePaths[0],
              pic:res.data
            })
            //do something
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})